<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class WechatGroup
 * @package App\Models
 */
class WechatGroup extends Model
{
    public static $groupRules = [
        'name' => 'required'
    ];

    protected $fillable = [
        'is_enable', 'name'
    ];

    public function scopeIsEnable($query)
    {
        return $query->where('is_enable', '=', 1);
    }

    public function scopeNameLike($query, $name)
    {
        $query->when($name, function ($query) use ($name) {
            $query->where('name', 'like', '%' . $name . '%');
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function wechatMenus()
    {
        return $this->hasMany(WechatMenu::class, 'group_id', 'id');
    }

    /**
     * @return array
     * 组装数据，供微信公众号应用菜单使用
     */
    public function assembleAppliedMenus()
    {
        $buttons = [];
        //获取数据库中所有已启用的菜单信息并且属于该活动（已经排好序）
        $data = $this->wechatMenus()->orderBy('sort', 'asc')->isEnable()->get()->toArray();
        //主菜单
        $mainBtns = [];
        //子菜单
        $btns = [];
        //记录含有子菜单的主菜单id
        $hasBtnIds = [];
        foreach ($data as $v) {
            if ($v['sub_id'] == 0) {
                array_push($mainBtns, $v);
            }else{
                array_push($btns, $v);
                array_push($hasBtnIds, $v['sub_id']);
            }
        }
        foreach ($mainBtns as $k => $v) {
            //分开主菜单 是否含有子菜单
            if (in_array($v['id'], $hasBtnIds)) {
                //含有子菜单的主菜单
                //先组合该主菜单下的子菜单列表
                $subButton = [];
                foreach ($btns as $vv) {
                    //找到属于当前主菜单的子菜单
                    if ($vv['sub_id'] == $v['id']) {
                        if ($vv['type'] == 'click') {
                            array_push($subButton, [
                                'name' => $vv['name'],
                                'type' => $vv['type'],
                                'key'  => $vv['key']
                            ]);
                        }elseif ($vv['type'] == 'view') {
                            array_push($subButton, [
                                'name' => $vv['name'],
                                'type' => $vv['type'],
                                'url'  => $vv['url']
                            ]);
                        }elseif ($vv['type'] == 'media_id') {
                            array_push($subButton, [
                                'name' => $vv['name'],
                                'type' => $vv['type'],
                                'media_id'  => $vv['media_id']
                            ]);
                        }elseif ($vv['type'] == 'miniprogram') {
                            array_push($subButton, [
                                'name' => $vv['name'],
                                'type' => $vv['type'],
                                'url'  => $vv['url'],
                                'appid'  => $vv['miniprogram_appid'],
                                'pagepath'  => $vv['pagepath']
                            ]);
                        }
                    }
                }
                array_push($buttons, [
                    'name' => $v['name'],
                    'sub_button' => $subButton
                ]);
            }else{
                //不含有
                if ($v['type'] == 'click') {
                    array_push($buttons, [
                        'name' => $v['name'],
                        'type' => $v['type'],
                        'key'  => $v['key']
                    ]);
                }elseif ($v['type'] == 'view') {
                    array_push($buttons, [
                        'name' => $v['name'],
                        'type' => $v['type'],
                        'url'  => $v['url']
                    ]);
                }elseif ($v['type'] == 'media_id') {
                    array_push($buttons, [
                        'name' => $v['name'],
                        'type' => $v['type'],
                        'media_id'  => $v['media_id']
                    ]);
                }elseif ($v['type'] == 'miniprogram') {
                    array_push($buttons, [
                        'name' => $v['name'],
                        'type' => $v['type'],
                        'url'  => $v['url'],
                        'appid'  => $v['miniprogram_appid'],
                        'pagepath'  => $v['pagepath']
                    ]);
                }
            }
        }
        return $buttons;
    }
}